package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseControlle;
import com.atguigu.entity.HouseUser;
import com.atguigu.service.HouseUserService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping(value="/houseUser")
public class HouseUserController extends BaseControlle {

    @Reference
    private HouseUserService houseUserService;


    @GetMapping("/create")
    public String create(ModelMap model,@RequestParam("houseId") Long houseId) {
        model.addAttribute("houseId",houseId);
        return "houseUser/create";
    }

    @PostMapping("/save")
    public String save(HouseUser houseUser) {

        houseUserService.insert(houseUser);

        return "common/successPage";
    }

    @GetMapping("/edit/{id}")
    public String edit(ModelMap model,@PathVariable Long id) {
        HouseUser houseUser = houseUserService.getById(id);
        model.addAttribute("houseUser",houseUser);
        return "houseUser/edit";
    }


    @PostMapping(value="/update")
    public String update(HouseUser houseUser) {

        houseUserService.update(houseUser);

        return "common/successPage";
    }

    @GetMapping("/delete/{houseId}/{id}")
    public String delete(ModelMap model,@PathVariable Long houseId, @PathVariable Long id) {
        houseUserService.delete(id);
        return "redirect:/house/" + houseId;
    }

}